Skip to content

feat(governance): add opt-in hierarchical grants, budgets and attribution - #71

Merged
ALikesToCode merged 3 commits into
feat/tenant-hierarchyfrom
feat/tenant-governance
Oct 9, 2026
Merged

ALikesToCode merged 3 commits into
feat/tenant-hierarchyfrom
feat/tenant-governance

Conversation

@ALikesToCode

Copy link
Copy Markdown
Owner

Summary

Adds opt-in hierarchical governance for requests in a verified workspace: organisation and team grants intersect with the key's own grants, budgets at every level are reserved together, and usage is attributed to the workspace. With TENANT_GOVERNANCE_ENABLED unset or off (the default), key controls, grants, budgets, reservations, auto-route eligibility and GET /v1/usage are byte-for-byte unchanged, and the new routes return 404. Requests outside a workspace are unchanged even with the flag on.

  • Settings: TENANT_GOVERNANCE_ENABLED. Empty means off; a malformed value is off with one warning that leaves the value out.
  • Grants intersect, never union: a model or tool is allowed only if the key allows it and every configured ancestor (team, organisation) allows it. An ancestor with no grant adds no restriction, and no level can widen another. Applied in key_controls.model_allowed/provider_allowed and intelligence_policy.eligible.
  • Roles: a static permission table. admin edits grants and budgets and reads all workspace usage, billing reads usage and budgets, and member reads its own usage. No role can raise another's.
  • Budgets:
    • Optional daily and monthly limits per organisation and team, in integer micro-USD.
    • BudgetService.check_and_reserve reserves every configured level plus the key's own limits in one atomic operation.
    • An over-limit level returns 429 tenant_budget_exceeded with the level, before dispatch. An unpriced request returns 503 unpriced_reservation.
    • Settlement is joint, and an unknown cost keeps its hold at every level. Workspace reservations use their own tg_ identities and do not touch the existing reservation store.
  • Administration: GET/PUT /admin/organisations/<org_id>/governance and .../teams/<team_id>/governance. These need an administrator session, CSRF, the matching role and If-Match (428 when missing, 412 when stale), and write audit rows.
  • Usage: GET /v1/usage adds a workspace breakdown for callers in a workspace: their own usage for members, and organisation and team totals for admin/billing. Estimates stay labelled as estimates.

Details are in docs/tenant-governance.md.

Wiring

  • services/key_controls.py, services/budget_service.py, services/intelligence_policy.py and routes/usage.py get small call sites into services/tenant_governance.py. Each returns the existing result while the flag is off or the request has no workspace.
  • The workspace and role come from injected collaborators, which default to the legacy tenant and no role. The Wave 7 integration connects them to the organisations feature, along with the private D1 endpoint (worker/tenant-governance-d1.mjs) and native Worker generations.

Shared registrations

  • tests/test_tenant_governance_worker.mjs in test:worker.
  • worker/tenant-governance-d1.mjs in the edge module inventory, and both governance routes in the Flask route inventory.
  • TENANT_GOVERNANCE_ENABLED in the Container env keys.
  • tenant_governance_policies, tenant_governance_reservations, tenant_governance_components, tenant_governance_baselines and tenant_governance_audit in REQUIRED_D1_TABLES.

Stacked on #70

Migration numbers must be contiguous in each PR, so this branch merges feat/tenant-hierarchy (#70, migration 0033). Merge that first. SAML federation stacks on this one.

Tests

  • tests/test_tenant_governance.py and tests/test_tenant_governance_worker.mjs cover:
    • grant intersection and denied role escalation;
    • atomic multi-level reserve, reject and settle;
    • a concurrent budget race;
    • unknown-cost holds;
    • legacy behaviour while off;
    • workspace usage isolation;
    • stale revisions returning 412.
  • Full Python suite: 4,815 passed, 2 skipped. ruff, mypy and bandit are clean.
  • npm run test:worker: 1,809 passed. npm run test:integration: 7 passed.
  • cf:migrations:check reports 34 contiguous, additive migrations. test:deploy, free routes, client headers and the wrangler deploy --dry-run bundle all pass.
  • tests/test_frontend_build.mjs fails locally because @tailwindcss/cli is not installed. It fails the same way on main.

Apply the migration before enabling the flag: npx wrangler d1 migrations apply multillm-intelligence --remote.

CI notes

  • GitHub python/node jobs: the dependency-audit steps fail on every branch, including main. fix(deps): clear the CI dependency audit advisories #42 fixes that.
  • Workers Builds: fails on this stack because the Wave 3 base adds the AdmissionCoordinator Durable Object migration, which wrangler versions upload cannot apply. This is expected until wrangler deploy runs on main.

This is part of Wave 7. The README line comes with the Wave 7 integration PR.

…tion

TENANT_GOVERNANCE_ENABLED (off by default) applies organisation and team
policies to requests in a verified workspace. Model and tool grants
intersect with the key's own grants and never widen them. Budgets at every
configured level, plus the key's own limits, are reserved together in one
atomic operation and settled together; an over-limit level denies the
request before dispatch, and an unknown cost keeps its hold at every level.

Roles come from a static permission table. Administrators edit policies
through /admin/organisations/<org_id>[/teams/<team_id>]/governance with
If-Match and audit rows, and /v1/usage gains a workspace breakdown for
callers in a workspace. Requests outside a workspace are unchanged
(migration 0034).
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Oct 9, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d330e486-1fe0-4f31-8c62-71af35867c53

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
multillm-proxy de9a471 Oct 09 2026, 07:59 AM

@ALikesToCode
ALikesToCode merged commit 41edfee into feat/tenant-hierarchy Oct 9, 2026
4 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant